<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()" ref="dataForm">
      <el-form-item prop="msgScope">
        <el-select v-model="dataForm.msgScope" placeholder="请选择推送范围">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="pushRate">
        <el-select v-model="dataForm.pushRate" placeholder="请选择推送频次">
          <el-option
            v-for="item in useroption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="msgName">
        <el-input v-model="dataForm.msgName" placeholder="请输入消息名称"></el-input>
      </el-form-item>
      <!-- <el-form-item>
      <template>
        <el-date-picker
          style="width:220px;"
          v-model="dataForm.pushStartTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="推送开始时间"
        ></el-date-picker>~
        <el-date-picker
          style="width:220px;"
          v-model="dataForm.pushEndTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="推送结束时间"
        ></el-date-picker>
      </template>
      </el-form-item>-->
      <el-form-item>
        <el-date-picker
          v-model="pushTime"
          type="datetimerange"
          value-format="yyyy-MM-dd HH:mm:ss"
          range-separator="至"
          start-placeholder="发送日期起"
          end-placeholder="发送日期止"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="search()">查询</el-button>
        <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button
          type="danger"
          @click="deleteHandle()"
          :disabled="dataListSelections.length <= 0"
        >批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      style="width: 100%;"
    >
      <el-table-column prop="msgName" header-align="center" align="center" label="消息名称"></el-table-column>
      <el-table-column prop="msgContext" header-align="center" align="center" label="消息内容"></el-table-column>
      <el-table-column prop="msgScope" header-align="center" align="center" label="推送范围">
        <template slot-scope="scope">
          {{scope.row.msgScope==0?'全部用户':''}}
          {{scope.row.msgScope==1?'非会员':''}}
          {{scope.row.msgScope==2?'会员':''}}
        </template>
      </el-table-column>
      <el-table-column prop="jumpLink" header-align="center" align="center" label="推送链接"></el-table-column>
      <el-table-column prop="pushRate" header-align="center" align="center" label="推送频次">
        <template slot-scope="scope">{{scope.row.pushRate==0?'单次':'每天'}}</template>
      </el-table-column>
      <el-table-column prop="pushTime" header-align="center" align="center" label="推送时间"></el-table-column>
      <el-table-column prop="pushState" header-align="center" align="center" label="推送状态">
        <template slot-scope="scope">
          <span style="color:#00f" v-if="scope.row.pushState===0">未推</span>
          <span v-if="scope.row.pushState===1">已推</span>
        </template>
      </el-table-column>
      <el-table-column prop="pushNum" header-align="center" align="center" label="推送人数"></el-table-column>
      <el-table-column prop="readNum" header-align="center" align="center" label="已读人数"></el-table-column>
      <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
              v-if="scope.row.pushRate=='1'||scope.row.pushState=='0'"
            >修改</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>
<style>
.disable {
  pointer-events: none;
  color: #ccc;
  text-decoration: underline;
  margin-right: 5px;
}
.disable .el-button--text {
  color: #ccc;
}
.active {
  cursor: pointer;
}
</style>

<script>
import AddOrUpdate from "./MessagePush-add";
export default {
  data() {
    return {
      options: [
        {
          value: null,
          label: "全部"
        },
        {
          value: "0",
          label: "全部用户"
        },
        {
          value: "1",
          label: "非会员"
        },
        {
          value: "2",
          label: "会员"
        }
      ],
      useroption: [
        {
          value: null,
          label: "全部"
        },
        {
          value: "0",
          label: "单次"
        },
        {
          value: "1",
          label: "每天"
        }
      ],
      dataForm: {
        msgName: "",
        msgScope: "",
        pushRate: ""
      },
      pushTime: [],
      isAlive: "",
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
      // show: "",
      // hide:"",
    };
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList();
  },
  methods: {
    search(){
      this.pageIndex = 1;
      this.getDataList();
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      var obj = {
        page: this.pageIndex,
        limit: this.pageSize,
        msgType: "0"
      };
      if (this.dataForm.msgName) {
        obj.msgName = this.dataForm.msgName;
      }
      if (this.dataForm.msgScope) {
        obj.msgScope = this.dataForm.msgScope;
      }
      if (this.dataForm.pushRate && this.dataForm.pushRate != null) {
        obj.pushRate = this.dataForm.pushRate + "";
      }
      if (this.pushTime.length > 0) {
        obj.pushStartTime = this.pushTime[0];
        obj.pushEndTime = this.pushTime[1];
      }
      // if (this.dataForm.pushStartTime) {
      //   obj.pushStartTime = this.dataForm.pushStartTime;
      // }
      // if (this.dataForm.pushEndTime) {
      //   obj.pushEndTime = this.dataForm.pushEndTime;
      // }
      this.$http({
        url: this.$http.adornUrl("/user/msgtemplate/list"),
        method: "get",
        params: this.$http.adornParams(obj)
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.$refs["dataForm"].resetFields();
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
          this.dataList.forEach(item => {
            if (item.pushRate == 1) {
              item.pushTime = item.pushTime.slice(10, 19);
            } else {
              item.pushTime = item.pushTime;
            }
          });
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map(item => {
            return item.id;
          });
      this.$confirm(
        `确定进行[${id ? "删除" : "批量删除"}]操作?`,
        // `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        this.$http({
          url: this.$http.adornUrl("/user/msgtemplate/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.getDataList();
              }
            });
          } else {
            this.$message.error(data.msg);
          }
        });
      });
    }
  }
};
</script>


